package com.iwinding.ladygg.ui.act;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

import com.iwinding.ladygg.R;
import com.iwinding.ladygg.net.RequestManager;
import com.iwinding.ladygg.net.callback.BaseRpCallback;
import com.iwinding.ladygg.net.request.ChangeMobileReq;
import com.iwinding.ladygg.net.response.BaseRopnse;
import com.iwinding.ladygg.utils.StringUtil;
import com.iwinding.ladygg.utils.ToastUtil;

/**
 * Created by hezhiyong on 2017/4/20.
 */

public class ConfirmPhoneActivity extends BaseActivity implements View.OnClickListener {

    private final int CHANGEPHONE = 104;

    AutoCompleteTextView completeTextView;

    @Override
    protected int getLayoutID() {
        return R.layout.icane_activity_confirm_phone;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.icane_confirm:
                confirm();
                break;
        }
    }

    void initView() {
        setTitleResId(R.string.icane_confirm_phone);

        completeTextView = (AutoCompleteTextView) findViewById(R.id.icane_edit_phone);
        findViewById(R.id.icane_confirm).setOnClickListener(this);
    }

    void confirm() {
        String phone = completeTextView.getEditableText().toString();

        if (TextUtils.isEmpty(phone)) {
            ToastUtil.openEmptyPhone(this);
            return;
        }
        if (!StringUtil.isMobileNO(phone)) {
            ToastUtil.openMolieErrorToast(this);
            return;
        }

        ChangeMobileReq req = new ChangeMobileReq();
        req.mobile = phone;

        RequestManager.postChangeMobileCheck(mContext, req, new BaseRpCallback<BaseRopnse>() {


            @Override
            public BaseRopnse parseResponse(BaseRopnse response, int id) {
                return response;
            }

            @Override
            public void onResult(BaseRopnse baseRopnse) {
                startActivityForResult(new Intent(mContext, ChangePhoneActivity.class), CHANGEPHONE);
            }

            @Override
            public void onFail(String errorMsg) {
                ToastUtil.openBaseToast(mContext, errorMsg, Toast.LENGTH_SHORT);
            }

            @Override
            public void onError(okhttp3.Call call, Exception e, int id) {
                ToastUtil.openVerFailToast(mContext);
            }
        });
//        LoginRp loginRp = SystemUtil.getLoginRp(mContext);
//        if (!TextUtils.equals(phone, loginRp.mobile)) {
//            ToastUtil.openBaseToast(mContext, R.string.incorrect_phone_number, Toast.LENGTH_SHORT);
//            return;
//        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == CHANGEPHONE) {
            if (resultCode == RESULT_OK) {
                finish();
            }
        }
    }
}
